package com.wocai.jrt.orders.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wocai.jrt.investor.service.spi.RiskDisclosureService;
import com.wocai.jrt.orders.model.Orders;
import com.wocai.jrt.orders.service.spi.ContactService;
import com.wocai.jrt.orders.service.spi.OrdersService;

public class ContactThread implements Runnable {
	private static final Logger LOGGER = LoggerFactory.getLogger(ContactThread.class);

	private ContactService contactServiceImpl;
	private RiskDisclosureService riskDisclosureServiceImpl;
	private String orderId;
	private String originalContact;
	private OrdersService ordersServiceImpl;

	public ContactThread(ContactService contactServiceImpl,
			RiskDisclosureService riskDisclosureServiceImpl, String orderId) {
		this.contactServiceImpl = contactServiceImpl;
		this.riskDisclosureServiceImpl = riskDisclosureServiceImpl;
		this.orderId = orderId;
	}

	public ContactThread(ContactService contactServiceImpl,
			RiskDisclosureService riskDisclosureServiceImpl, String orderId,
			OrdersService ordersServiceImpl, String originalContact) {
		this.contactServiceImpl = contactServiceImpl;
		this.riskDisclosureServiceImpl = riskDisclosureServiceImpl;
		this.orderId = orderId;
		this.ordersServiceImpl = ordersServiceImpl;
		this.originalContact = originalContact;
	}

	@Override
	public void run() {
		try {
			LOGGER.info("start up contact creating thread!");
			if (null != contactServiceImpl) {
				contactServiceImpl.create(orderId);
			}

			if (null != riskDisclosureServiceImpl) {
				riskDisclosureServiceImpl.create(orderId);
			}
			LOGGER.info("excute contact creating thread success!");
		} catch (Exception e) {
			if (StringUtils.isNotBlank(originalContact)) {
				Orders order = new Orders();
				order.setPaySignUser(originalContact);

				Orders condition = new Orders();
				condition.setOrderId(orderId);
				ordersServiceImpl.updateBulks(order, condition);
			}
			LOGGER.error("contact thread occured exception, msg=[{}]", e.getMessage());
		}
	}

}
